/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame;

import java.awt.Color;
import java.util.LinkedList;
import cardgame.game.*;

/**
 *
 * @author Carl
 */
public class ManaChooser extends CardChooser{

    public ManaChooser(CardChooserManager cardChooserManager){
        super(cardChooserManager);
    }
    public enum ManaChooseType{
        MONSTER_SUMMON_COST,
        MONSTER_SPELL_COST,
        SPELL_COST
    }
    private ManaChooseType type;
    private int playerIndex;
    private int[] chooseInformation;
    private ManaAmount remainingAmount;
    private LinkedList<Integer> manaIndices = new LinkedList<Integer>();

    public void start(ManaChooseType type, int playerIndex, int[] chooseInformation, ManaAmount manaAmount){
        this.type = type;
        this.playerIndex = playerIndex;
        this.chooseInformation = chooseInformation;
        remainingAmount = manaAmount.clone();
        manaIndices.clear();
        updateMessage();
    }
    
    @Override
    public boolean isValidChoice(Card card){
        if((card.getOwner().getIndex() == playerIndex) && (card.getCardPosition().getZone() == CardPosition.Zone.MANA)){
            if(card instanceof ManaProvider){
                ManaProvider manaProvider = (ManaProvider) card;
                if(manaProvider.isAvailable() && remainingAmount.canBePartlyFilledWith(manaProvider.getProvidedMana())){
                    return (!manaIndices.contains(card.getCardPosition().getIndex()));
                }
            }
        }
        return false;
    }
    
    public void addManaProvider(Card card){
        manaIndices.add(card.getCardPosition().getIndex());
        ManaProvider manaProvider = (ManaProvider) card;
        remainingAmount.subtract(manaProvider.getProvidedMana());
        if(!isFinished()){
            updateMessage();
        }
    }
    
    private void updateMessage(){
        cardChooserManager.setMessage("Please choose " + getManaHTML(remainingAmount) + " mana.");
    }
    
    public static String getManaHTML(ManaAmount manaAmount){
        String html = "";
        boolean isFirstMana = true;
        for(int i=0;i<Mana.values().length;i++){
            Mana mana = Mana.values()[i];
            int amount = manaAmount.getMana(mana);
            if(amount > 0){
                Color manaColor = ManaAmount.getColor(mana);
                if(!isFirstMana){
                    html += ", ";
                }
                //html += "<span style=\"font-weight:bold; color:rgb(" + manaColor.getRed() + "," + manaColor.getGreen() + "," + manaColor.getBlue() + ");" + ((mana == Mana.WHITE)?" background:#000;":"") + "\">" + amount + "</span>";
                html += amount + " " + mana.toString().toLowerCase();
                isFirstMana = false;
            }
        }
        return html;
    }
    
    public boolean isFinished(){
        return remainingAmount.isEmpty();
    }
    
    @Override
    public void stop(){
        super.stop();
        remainingAmount = null;
    }

    public ManaChooseType getType(){
        return type;
    }

    public int[] getChooseInformation(){
        return chooseInformation;
    }

    public int[] getManaIndices(){
        return Util.convertToArray(manaIndices);
    }
}
